﻿using System;
using System.CodeDom;
using System.Linq;
using System.Net;
using System.Runtime.Remoting;
using Imgur.Net.Auth;
using NUnit.Framework;
using TestableHttpWebResponse;

namespace Imgur.Net.Test.ImageProvider
{
    [TestFixture]
    public class GetAlbumImageInfoTests
    {

        private const string albumId = "PEVrZ";
        private const string imageId = "hdpr22d";

        private const string clientId = "b56c4ebcd84a2c5";
        private const string clientSecret = "5b83ef6e0100a5a8544cc03f2e49c470d7f7cb2f";

        private const string baseTestUriTemplate = "test://api.imgur.com/3/album";

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            WebRequest.RegisterPrefix("test", TestableWebRequestCreateFactory.GetFactory());
        }

        [Test]
        public void TestGetBasicAlbumImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            
            
            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}/image/{1}", albumId, imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumImageInfo.basic.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);
           
            ImageInfo info = albumProvider.GetAlbumImageInfo("PEVrZ", "hdpr22d");

            Assert.IsNotNull(info);
            Assert.IsNull(info.AccountUrl);
            Assert.IsNotNull(info.IsAnimated);
            Assert.IsFalse(info.IsAnimated.Value);
            Assert.AreEqual(720478, info.Bandwidth);
            Assert.AreEqual(new DateTime(2014, 11, 01, 14, 10, 10), info.TimeStamp);
            Assert.IsNull(info.Description);
            Assert.IsNotNull(info.IsFavorite);
            Assert.IsFalse(info.IsFavorite.Value);
            Assert.AreEqual(1600, info.Height);
            Assert.AreEqual("hdpr22d", info.Id);
            Assert.AreEqual("http://i.imgur.com/hdpr22d.jpg", info.Link);
            Assert.IsNull(info.IsNSFW);
            Assert.IsNull(info.Section);
            Assert.AreEqual(360239, info.FileSize);
            Assert.IsNull(info.Title);
            Assert.AreEqual("image/jpeg", info.MimeType);
            Assert.AreEqual(2, info.Views);
            Assert.AreEqual(2560, info.Width);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "album ID is requred.")]
        public void TestNullAlbumIdGetAlbumImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);


            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}/image/{1}", albumId, imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.basic.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = albumProvider.GetAlbumImageInfo(null, "hdpr22d");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "image ID is requred.")]
        public void TestNullImageIdGetAlbumImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);


            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}/image/{1}", albumId, imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.basic.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = albumProvider.GetAlbumImageInfo("12345", null);
        }
    }
}
